<?php
/**
 * Created by PhpStorm.
 * User: m
 * Date: 2018/2/23
 * Time: 23:52
 */

namespace core;
class imooc
{
    public static  $classMap = array();
    public $assign;
    static public  function run(){
        \core\lib\log::init();
        $route = new \core\lib\route();
        $ctrlClass = $route->ctrl;
        $action = $route->action;
        $ctrlfile = APP.'/ctrl/'.$ctrlClass.'Ctrl.php';
        $ctrlClass = '\\'.MODULE.'\ctrl\\'.$ctrlClass.'Ctrl';

        if (is_file($ctrlfile)){
            include $ctrlfile;
          $ctrl =  new $ctrlClass();
          $ctrl->$action();
          \core\lib\log::log('ctrl:'.$ctrlClass.'    '.'action:'.$action);
        }else{
            throw new \Exception('找不到控制器'.$ctrlClass);
        }

    }

    static public function load($class)
    {
        //自动加载类库
        if (isset($classMap[$class])) {
            return true;
        } else {
            $class = str_replace('\\', '/', $class);
            $file = XIAOFENG .'/'. $class . '.php';
            if (is_file($file)) {
                include $file;
                self::$classMap[$class] = $class;
            } else {
                return false;
            }
        }
    }

    public function assign($name,$value){

        $this->assign[$name] = $value;
    }

    public function display($file){

        $pfile = $file;
        $file = APP.'/views/'.$file;

        if (is_file($file)){
           // extract($this->assign);
            //include $file;
            \Twig_Autoloader::register();
            $loader = new \Twig_Loader_Filesystem(APP.'/views');
            $twig = new \Twig_Environment($loader,array(
                'cache'=>XIAOFENG.'/log/twig',
                'debug'=>DEBUG
            ));
            $template = $twig->loadTemplate($pfile);
            $template->display($this->assign?$this->assign:"");
        }
    }

}